package com.black.cat.jvm.classfile.attribute;


import com.black.cat.jvm.classfile.ClassReader;
import com.black.cat.jvm.classfile.constantpool.ConstantPool;

/**
 * @Author blackcat
 * @create 2021/8/11 20:40
 * @version: 1.0
 * @description: ```
 * SourceFile_attribute {
 * u2 attribute_name_index;
 * u4 attribute_length;//必须2
 * u2 sourcefile_index;//常量池索引，指向CONSTANT_Utf8_info常量
 * }
 * ```
 */
public class SourceFileAttribute implements AttributeInfo {

    private ConstantPool cp;
    private long attributeLength;
    private String sourceFile;

    public SourceFileAttribute(ConstantPool cp, long attrLen) {
        this.cp = cp;
        this.attributeLength = attrLen;
    }

    @Override
    public void readInfo(ClassReader reader) {
        int sourceFileIndex = reader.nextU2ToInt();
        sourceFile = cp.getUtfStringByIndex(sourceFileIndex);
    }

    @Override
    public String toString() {
        return "SourceFileAttribute{" +
                "attributeLength=" + attributeLength +
                "sourceFile=" + sourceFile +
                "}";
    }
}
